//
// Created by LONG on 2020/10/16.
//

#include <stdio.h>
#include <NGAP_PDUSessionResourceSetupResponseTransfer.h>
#include <NGAP_AssociatedQosFlowItem.h>
#include <NGAP_GTPTunnel.h>
#include <NGAP_NGSetupRequest.h>
#include <NGAP_ProtocolIE-Field.h>
#include <NGAP_GlobalGNB-ID.h>
#include <NGAP_InitialUEMessage.h>
#include "../../../lib/doctest.h"
#include "../../../../src/ccore/asn1c/codec/asn1Codec.h"
#include "../../../common/commDef.h"
#include "../../../../lib/csp/include/osal/osalApi.h"

TEST_SUITE ("ASN1_TS")
{
    TEST_CASE ("ASN1_TC")
    {
        int32_t rc;
        asn_enc_rval_t asnEncRc;
        asn_dec_rval_t asnDecRc;
        char buffer[64 * 1024];
//        SUBCASE("ASN1_SC_BASE")
//        {
//            NGAP_PDUSessionResourceSetupResponseTransfer_t message;
//
//            NGAP_QosFlowPerTNLInformation_t *dLQosFlowPerTNLInformation = NULL;
//
//            NGAP_UPTransportLayerInformation_t *uPTransportLayerInformation = NULL;
//            NGAP_GTPTunnel_t *gTPTunnel = NULL;
//
//            NGAP_AssociatedQosFlowList_t *associatedQosFlowList = NULL;
//            NGAP_AssociatedQosFlowItem_t *associatedQosFlowItem = NULL;
//
//            memset(&message, 0, sizeof(message));
//
//            dLQosFlowPerTNLInformation = &message.dLQosFlowPerTNLInformation;
//            uPTransportLayerInformation =
//                &dLQosFlowPerTNLInformation->uPTransportLayerInformation;
//
//            gTPTunnel = (NGAP_GTPTunnel_t*)(calloc(1, sizeof(struct NGAP_GTPTunnel)));
//            uPTransportLayerInformation->present =
//                NGAP_UPTransportLayerInformation_PR_gTPTunnel;
//            uPTransportLayerInformation->choice.gTPTunnel = gTPTunnel;
//
//            associatedQosFlowList = &dLQosFlowPerTNLInformation->associatedQosFlowList;
//            associatedQosFlowItem =
//                (NGAP_AssociatedQosFlowItem_t*)(calloc(1, sizeof(struct NGAP_AssociatedQosFlowItem)));
//            ASN_SEQUENCE_ADD(&associatedQosFlowList->list, associatedQosFlowItem);
//
//            asnRc = aper_encode_to_buffer(&asn_DEF_NGAP_PDUSessionResourceSetupResponseTransfer, NULL, &message, buffer, sizeof(buffer));
//            TEST_LOG("aper_encode_to_buffer() rc = %ld, %s\n", asnRc.encoded, asnRc.failed_type->name);
//        }
//
//        SUBCASE("ASN1_SC_NGSetupRequest")
//        {
//            NGAP_NGSetupRequest_t message;
//            memset(&message, 0, sizeof(message));
//
//            NGAP_GlobalGNB_ID_t *globalGNB_ID = (NGAP_GlobalGNB_ID_t *)CALLOC(1, sizeof(NGAP_GlobalGNB_ID_t));
//            globalGNB_ID->gNB_ID.present = NGAP_GNB_ID_PR_gNB_ID;
//            globalGNB_ID->gNB_ID.choice.gNB_ID.buf = (uint8_t *)"11111111";
//            globalGNB_ID->gNB_ID.choice.gNB_ID.size = 8;
//            globalGNB_ID->pLMNIdentity.buf = (uint8_t *)"2223_plmn";
//            globalGNB_ID->pLMNIdentity.size = 3;
//
//            NGAP_NGSetupRequestIEs_t *ngSetupRequestIEs = (NGAP_NGSetupRequestIEs_t*)(calloc(1, sizeof(NGAP_NGSetupRequestIEs_t)));
//            ngSetupRequestIEs->id = NGAP_ProtocolIE_ID_id_GlobalRANNodeID;
//            ngSetupRequestIEs->criticality = NGAP_Criticality_reject;
//            ngSetupRequestIEs->value.present = NGAP_NGSetupRequestIEs__value_PR_GlobalRANNodeID;
//            ngSetupRequestIEs->value.choice.GlobalRANNodeID.present = NGAP_GlobalRANNodeID_PR_globalGNB_ID;
//            ngSetupRequestIEs->value.choice.GlobalRANNodeID.choice.globalGNB_ID = globalGNB_ID;
//
//            ASN_SEQUENCE_ADD(&message.protocolIEs, ngSetupRequestIEs);
//            asnRc = aper_encode_to_buffer(&asn_DEF_NGAP_NGSetupRequest, NULL, &message, buffer, sizeof(buffer));
//            TEST_LOG("aper_encode_to_buffer() rc = %ld, %s\n", asnRc.encoded, asnRc.failed_type->name);
//        }
//
        SUBCASE("ASN1_SC_NGInitialUEMessage")
        {
            REQUIRE(OS_MemGetAllocCount() == OS_MemGetFreeCount());

            NGAP_InitialUEMessage_t	 message;
            memset(&message, 0, sizeof(message));

            NGAP_InitialUEMessage_IEs_t *ie = NULL;

            ie = (NGAP_InitialUEMessage_IEs_t *)ASN1_MemCalloc(1, sizeof(NGAP_InitialUEMessage_IEs_t));
            ASN_SEQUENCE_ADD(&message.protocolIEs, ie);

            ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
            ie->criticality = NGAP_Criticality_reject;
            ie->value.present = NGAP_InitialUEMessage_IEs__value_PR_RAN_UE_NGAP_ID;
            ie->value.choice.RAN_UE_NGAP_ID = 100;

            asnEncRc = aper_encode_to_buffer(&asn_DEF_NGAP_InitialUEMessage, NULL, &message, buffer, sizeof(buffer));
            REQUIRE(-1 != asnEncRc.encoded);
            REQUIRE(OS_MemGetAllocCount() == OS_MemGetFreeCount());

            void *pstr = NULL;
            asnDecRc = aper_decode(NULL, &asn_DEF_NGAP_InitialUEMessage, (void **)&pstr,
                buffer, asnEncRc.encoded, 0, 0);
            REQUIRE(RC_OK == asnDecRc.code);
            OS_MemFree(pstr);
            REQUIRE(OS_MemGetAllocCount() == OS_MemGetFreeCount());
        }
    }
}

